Jenkins Kata 6: Deploying Containers (Step 4–6)

Learn about adding a build pipeline, defining and configuring a Dev, QA, and production environment deployment job, and adding a pipeline view.

Step 4: Define a QA environment deployment job#

The following are the steps to define a QA environment deployment job:

  • Return to the Jenkins tab.
  • Click the “Jenkins” icon at the top left.
  • Click “New Item.”
  • Enter “qa-deploy-storelist” in the item name field.
  • Select “Freestyle project.”
  • Click “OK.”
Job creation for qa-deploy-storelist
Job creation for qa-deploy-storelist
  • Click the “Build Steps” tab.
  • Select “Add build step.”
  • Select “Execute shell.”
  • Enter the following commands:
  • Click “Save”
Adding commands in the “Command” field
Adding commands in the “Command” field

Commands

Command / Parameter

Description

docker container

This is the Docker parent command for containers.

rm

This removes a container.

-f

This forces the removal of a container even if it's running. If the container is running, it's killed and then removed.

qa-storelist

This is the name of the container to remove.




|| true

The double-pipe character is a “logicial OR.”

If the `qa-storelist` container is not running, the Docker `rm` command will fail. This is expected; however, it would cause the command to return a failure code, causing the job to fail. The OR statement returns true from the command even if the docker `rm` command fails.

This ensures that this command will return a success code, whether the `qa-storelist` container was running or not.

docker container

This is the Docker parent command for containers.

run

This runs a new container.

-d

This runs a disconnected container.

-p

This maps ports from the host to the container.

8002:80

This maps port 8002 on the host to port 80 in the container.

--net=storelist-net

This assigns a user-defined Docker bridge network to the container.

--ip=172.20.0.15

This assigns a static IP to the container within the subnet range defined on the user-defined network.

--name

This assigns a name to the container.

qa-storelist

This is the name assigned to the container.

ed-6091404232622080.educative.run/dk/storelist

This is the URL to the private repository and the name of the image to run.

  • Click “Build Now.”
Click “Build Now”
Click “Build Now”
  • Click back to “Dashboard.”
  • Select the “dev-deploy-storelist” job.
  • Click “Configure.”
  • Click the “Post-build Actions” tab.
  • Select “Add post-build action.”
  • Select “Build other projects (manual step).”
  • Enter “qa-deploy-storelist” in the “Downstream Project Names” field.
  • Click “Save.”
Select "Build other projects (manual step)" from the “Post-build Actions” tab
Select "Build other projects (manual step)" from the “Post-build Actions” tab
Add “qa-deploy-storelist” in the “Downstream Project Names” field
Add “qa-deploy-storelist” in the “Downstream Project Names” field

This step connects the dev-deploy-storelist job to the qa-deploy-storelist job. The configuration is very similar to the previous step, with one key difference: the dev-deploy-storelist job is now configured to execute qa-deploy-storelist as a “Build other projects” manual post-build step instead of an automated step. The manual post-build step was added by the Build Pipeline plugin. We’ll have an opportunity to see the effect of a manual post-build step at the end of this kata when we add a pipeline view.

The manual post-build action requires human intervention to execute. Depending on the application and organizational policies, some deployments can be fully automated. Some deployments, however, may still require approvals from IT staff or management. This configuration represents a deployment pipeline in which a release to the QA environment requires human approval.

Step 5: Define a production deployment job#

The following are the steps to define a production deployment job:

  • Click back to “Dashboard.”
  • Click “New Item.”
  • Enter “prod-deploy-storelist” in the “Enter an item name” field.
  • Select “Freestyle project.”
  • Click “OK.”
Enter “prod-deploy-storelist” in the “item name” field and select “Freestyle project”
Enter “prod-deploy-storelist” in the “item name” field and select “Freestyle project”
  • Click the “Build Steps” tab.
  • Select “Add build step.”
  • Select “Execute shell.”
  • Enter the following commands:
  • Click “Save.”
Adding commands in the “Command” field
Adding commands in the “Command” field

Commands

Command / Parameter

Description

docker container

This is the Docker parent command for containers.

rm

This removes a container.

-f

This forces the removal of a container even if it's running. If the container is running, it's killed and then removed.

prod-storelist

This is the name of the container to remove.




|| true

The double-pipe character is a “logical OR.”

If the qa-storelist container is not running, the Docker `rm` command will fail. This is expected; however, it would cause the command to return a failure code, causing the job to fail. The OR statement returns true from the command even if the docker rm command fails.

This ensures that this command will return a success code whether the qa-storelist container was running or not.

docker container

This is the Docker parent command for containers.

run

This runs a new container.

-d

This runs a disconnected container.

-p

This runs a disconnected container.

8003:80

This maps port 8003 on the host to port 80 in the container.

--net=storelist-net

This assigns a user-defined Docker bridge network to the container.

--ip=172.18.0.20

This assigns a static IP to the container within the subnet range defined on the user-defined network.

--name

This assigns a name to the container.

prod-storelist

This is the name assigned to the container.

localhost:5000/dk/storelist

This is the URL to the private repository and the name of the image to run.

  • Click “Build Now.”
Click “Build Now”
Click “Build Now”
  • Click back to “Dashboard.”
  • Select the “qa-deploy-storelist” job
  • Click “Configure.”
  • Click the “Post-build Actions” tab.
  • Click “Add post-build action.”
  • Select “Build other projects (manual step).”
  • Enter “prod-deploy-storelist” in the “Downstream Project Name” field.
  • Click “Save.”
Select "Build other projects (manual step)" from the “Post-build Actions” tab
Select "Build other projects (manual step)" from the “Post-build Actions” tab
Add “prod-deploy-storelist” in “Downstream Projects Name”
Add “prod-deploy-storelist” in “Downstream Projects Name”

The CD pipeline is now complete. Releases to the Dev environment are automated. Releases to QA and Prod require human approval.

  • Return to the terminal window.
  • List all the running containers as follows:
docker container ls
Listing all the running containers

There are now four containers running: one for each environment and the private Docker registry.

Step 6: Add a pipeline view#

The following are the steps to add a pipeline view:

  • Click the plus (“+”) sign next to the “All” view tab.
  • Enter “storelist-pipeline” in the “Name” field.
  • Select “Build Pipeline View.”
  • Click “OK.”
Click the "+" sign
Click the "+" sign
Enter “storelist-pipeline” in the “Name” field
Enter “storelist-pipeline” in the “Name” field
  • Find the “Pipeline Flow” section.
  • Set the “Select Initial Job” to “web-storelist.”
  • Click “OK.”
Select “web-storelist” as the initial job
Pipeline view
  • Click the trigger icon in the qa-deploy-storelist job.
Click the trigger icon
Pipeline view
  • Click the trigger icon in the prod-deploy-storelist job.
Click the trigger icon
Pipeline view

The Build Pipeline plugin adds a new view type to Jenkins. This view type presents a visual representation of the relationship between the build and deployment jobs created in this kata.

When one job A is configured to execute another job B, job B is a downstream job of job A. You can see this relationship in the order of the cards that represent each job. The “Run” button at the top of the view will start a new build pipeline execution starting with the web-storelist job. This executes web-storelist and then dev-deploy-storelist automatically, as configured.

The qa-deploy-storelist and prod-deploy-storelist jobs must be executed manually. Jenkins can be extended and configured to notify users when a build is ready to be manually executed as part of a build pipeline and to require approval for the execution from specific users.

Jenkins Kata 6: Deploying Containers (Steps 1–3)

Quiz Yourself on Jenkins Katas